<template>
  <div class="app-container">
    <div>
      <el-input
        v-model="viewModel.Name"
        size="small"
        placeholder="名称"
        style="width: 200px;"
        class="filter-item"
        @keyup.enter.native="queryList"
      />
      <el-select v-model="viewModel.CategoryId" filterable size="small" placeholder="请选择">
        <el-option
          v-for="item in queryCategoryList"
          :key="item.value"
          :label="item.label"
          :value="item.value"
        ></el-option>
      </el-select>

      <el-select v-model="viewModel.TypeCode" filterable size="small" placeholder="请选择">
        <el-option
          v-for="item in queryTypeList"
          :key="item.value"
          :label="item.label"
          :value="item.value"
        ></el-option>
      </el-select>
      <el-button type="primary" size="small" @click="queryList" icon="el-icon-search">查询</el-button>
      <el-button type="primary" size="small" @click="handleAdd" icon="el-icon-plus">新增</el-button>
      <el-button type="primary" size="small" @click="refresh" icon="el-icon-refresh">刷新</el-button>
    </div>

    <el-row :gutter="30" v-loading="loading" v-if="total>0">
      <el-col :span="6" v-for="m in modelList" :key="m.ID" style="margin-top: 20px;">
        <el-card :body-style="{ padding: '0px' }">
          <img :src="m.ShowCoverUrl" class="image-c" />
          <div style="padding: 14px;">
            <span
              style="text-overflow:ellipsis !important;white-space:nowrap !important;overflow:hidden !important;width: 100% !important;"
              :title="m.Name"
            >{{ m.Name }}</span>
            <div class="bottom clearfix">
              <span class="time">分类：{{ m.CategoryName}} || 班级分类：{{ m.TypeName}}</span>
              <div style="text-align: right;">
                <el-button
                  type="text"
                  size="mini"
                  icon="el-icon-scissors"
                  @click="guanlianUser(m)"
                >分配用户</el-button>
                <el-button type="text" size="mini" icon="el-icon-scissors" @click="guanlian(m)">关联课程</el-button>
                <el-button type="text" size="mini" icon="el-icon-check" @click="handleEdit(m)">编辑</el-button>
                <el-button type="text" size="mini" icon="el-icon-delete" @click="handleDelete(m)">删除</el-button>
              </div>
            </div>
          </div>
        </el-card>
      </el-col>
    </el-row>

    <el-row :gutter="30" v-loading="loading" v-else>暂无数据</el-row>

    <!-- 分页组件：https://element.eleme.cn/#/zh-CN/component/pagination -->
    <div class="block" style="float:right;margin-top:15px;" v-if="total>0">
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="viewModel.PageIndex"
        :page-sizes="[8, 16, 24, 40,80]"
        :page-size="viewModel.pageSize"
        @prev-click="lastPage"
        @next-click="nextPage"
        background
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      ></el-pagination>
    </div>

    <el-dialog
      :visible.sync="dialogVisible"
      :title="dialogType==='edit'?'编辑':'新增'"
      @close="closeDialog"
      width="50%"
      top="2vh"
      :close-on-click-modal="false"
    >
      <el-form
        :model="model"
        ref="model"
        :rules="rules"
        label-width="80px"
        label-position="right"
        style="width: 90%;"
      >
        <el-row>
          <el-col :span="12">
            <el-form-item label="分类" prop="CategoryId">
              <el-select
                v-model="model.CategoryId"
                style="width:100%"
                filterable
                placeholder="请选择分类"
              >
                <el-option
                  v-for="item in categoryList"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="班级类型" prop="TypeCode">
              <el-select v-model="model.TypeCode" style="width:100%" placeholder="请选择班级类型">
                <el-option
                  v-for="item in typeList"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="24">
            <el-form-item label="名称" prop="Name">
              <el-input v-model="model.Name" placeholder="名称" />
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="12">
            <el-form-item label="封面" prop="ShowCoverUrl">
              <el-upload
                class="avatar-uploader"
                :action="actionUrl"
                :show-file-list="false"
                :on-success="handleAvatarSuccess"
                :before-upload="beforeAvatarUpload"
              >
                <img v-if="model.ShowCoverUrl" :src="model.ShowCoverUrl" class="avatar" />
                <i v-else class="el-icon-plus avatar-uploader-icon"></i>
              </el-upload>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="12">
            <el-form-item label="原价" prop="OldPrice">
              <el-input-number
                v-model="model.OldPrice"
                style="width:100%"
                :min="0"
                :step="0.5"
                label="原价"
              ></el-input-number>
            </el-form-item>
          </el-col>

          <el-col :span="12">
            <el-form-item label="现价" prop="NowPrice">
              <el-input-number
                v-model="model.NowPrice"
                style="width:100%"
                :min="0"
                :step="0.5"
                label="现价"
              ></el-input-number>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="24">
            <el-form-item label="描述" prop="Description">
              <el-input type="textarea" :rows="3" v-model="model.Description" placeholder="描述" />
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="12">
            <el-form-item label="序号" prop="Sort">
              <el-input-number v-model="model.Sort" :min="0" :step="0.5" label="序号"></el-input-number>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div style="text-align:right;">
        <el-button type="danger" @click="dialogVisible=false">取消</el-button>
        <el-button type="primary" :loading="loading" @click="confirmModel">提交</el-button>
      </div>
    </el-dialog>

    <el-dialog
      :visible.sync="dialogVideoVisible"
      :title="'关联课程'"
      width="50%"
      top="4vh"
      :close-on-click-modal="false"
    >
      <el-tabs v-model="activeName" type="card" @tab-click="handleClick">
        <el-tab-pane :label="t.label" v-for="t in tabs" :key="t.value" :name="t.value">
          <div>
            <el-input
              v-model="videoViewModel.VideoName"
              size="small"
              placeholder="名称"
              style="width: 200px;"
              class="filter-item"
              @keyup.enter.native="queryVideoList"
            />
            <el-button type="primary" size="small" @click="queryVideoList" icon="el-icon-search">查询</el-button>
            <el-button type="primary" size="small" @click="refreshVideo" icon="el-icon-refresh">刷新</el-button>
          </div>
          <el-table
            :data="videoList"
            :header-cell-style="{background:'#eef1f6',color:'#606266'}"
            style="width: 100%;margin-top:30px;"
            border
            v-loading="videoLoading"
            @selection-change="handleSelectionChange"
          >
            <el-table-column type="selection" width="55"></el-table-column>
            <el-table-column label="名称" prop="Name">
              <template slot-scope="scope">{{ scope.row.Name }}</template>
            </el-table-column>
            <el-table-column label="描述" prop="Description">
              <template slot-scope="scope">{{ scope.row.Description }}</template>
            </el-table-column>

            <el-table-column label="分类名称" prop="CategoryName">
              <template slot-scope="scope">{{ scope.row.CategoryName }}</template>
            </el-table-column>

            <el-table-column label="基础分类" prop="TypeName">
              <template slot-scope="scope">{{ scope.row.TypeName }}</template>
            </el-table-column>

            <el-table-column label="状态" prop="StatusStr">
              <template slot-scope="scope">{{ scope.row.StatusStr }}</template>
            </el-table-column>
          </el-table>
          <!-- 分页组件：https://element.eleme.cn/#/zh-CN/component/pagination -->
          <div class="block" style="float:right;margin-top:15px;" v-if="total>0">
            <el-pagination
              @size-change="handleVideoSizeChange"
              @current-change="handleVideoCurrentChange"
              :current-page="videoViewModel.PageIndex"
              :page-sizes="[10, 20, 30, 40,100]"
              :page-size="videoViewModel.pageSize"
              background
              layout="total, sizes,  pager,  jumper"
              :total="videoTotal"
            ></el-pagination>
          </div>
        </el-tab-pane>
      </el-tabs>

      <span slot="footer" class="dialog-footer">
        <el-button @click="cancelForm">取 消</el-button>
        <el-button type="primary" @click="saveGuanlian">{{activeName=="1"?"取消关联":"保存关联"}}</el-button>
      </span>
    </el-dialog>

    <el-dialog
      :visible.sync="dialogUserVisible"
      :title="'分配用户'"
      width="50%"
      top="4vh"
      :close-on-click-modal="false"
    >
      <el-tabs v-model="activeUserName" type="card" @tab-click="handleUserClick">
        <el-tab-pane :label="t.label" v-for="t in tabs" :key="t.value" :name="t.value">
          <div>
            <el-input
              v-model="userViewModel.UserName"
              size="small"
              placeholder="名称"
              style="width: 200px;"
              class="filter-item"
              @keyup.enter.native="queryUserList"
            />
            <el-input
              v-model="userViewModel.LoginName"
              size="small"
              placeholder="登录名"
              style="width: 200px;"
              class="filter-item"
              @keyup.enter.native="queryUserList"
            />
            <el-button type="primary" size="small" @click="queryUserList" icon="el-icon-search">查询</el-button>
            <el-button type="primary" size="small" @click="refreshUser" icon="el-icon-refresh">刷新</el-button>
          </div>
          <el-table
            :data="userList"
            :header-cell-style="{background:'#eef1f6',color:'#606266'}"
            style="width: 100%;margin-top:30px;"
            border
            v-loading="userLoading"
            height="400"
            @selection-change="handleUserSelectionChange"
          >
            <el-table-column type="selection" width="55"></el-table-column>
            <el-table-column label="名称" prop="UserName">
              <template slot-scope="scope">{{ scope.row.UserName }}</template>
            </el-table-column>
            <el-table-column label="登录名" prop="LoginName">
              <template slot-scope="scope">{{ scope.row.LoginName }}</template>
            </el-table-column>

            <el-table-column label="手机号" prop="MobilePhone">
              <template slot-scope="scope">{{ scope.row.MobilePhone }}</template>
            </el-table-column>
          </el-table>
          <!-- 分页组件：https://element.eleme.cn/#/zh-CN/component/pagination -->
          <div class="block" style="float:right;margin-top:15px;" v-if="userTotal>0">
            <el-pagination
              @size-change="handleUserSizeChange"
              @current-change="handleUserCurrentChange"
              :current-page="userViewModel.PageIndex"
              :page-sizes="[10, 20, 30, 40,100]"
              :page-size="userViewModel.pageSize"
              background
              layout="total, sizes,  pager,  jumper"
              :total="userTotal"
            ></el-pagination>
          </div>
        </el-tab-pane>
      </el-tabs>

      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogUserVisible=false">关闭</el-button>
        <el-button type="primary" @click="saveUserGuanlian">{{activeUserName=="1"?"取消关联":"保存关联"}}</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<style>
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
  width: 100%;
}

.avatar-uploader .el-upload:hover {
  border-color: #409eff;
}

.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}

.avatar {
  width: 178px;
  height: 178px;
  display: block;
}

.v-modal {
  z-index: 2000 !important;
}

.time {
  font-size: 13px;
  color: #999;
}

.bottom {
  margin-top: 13px;
  line-height: 12px;
}

.image-c {
  width: 100%;
  height: 240px;
  display: block;
}

.clearfix:before,
.clearfix:after {
  display: table;
  content: "";
}

.clearfix:after {
  clear: both;
}
</style>

<script src="@/js/epackage/index.ts">
</script>
